<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="SSM 复习总结, Zhou•Yong">
    <meta name="description" content="Springspring 项目中对象容器，帮我们管理项目中的所有对象Spring 就是一个大工厂，可以将所有对象创建和依赖关系维护，交给 Spring 管理AOP 编程的支持Spring 提供面向切面编程，可以方便的实现对程序进行权限拦截、">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>SSM 复习总结 | Zhou•Yong</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

</head>

<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">Zhou•Yong</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>主页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>个人介绍</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>推荐</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Zhou•Yong</div>
        <div class="logo-desc">
            
            你迈出的步伐不非得很大，只要你是在正确的方向前进就行
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                主页
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                个人介绍
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                推荐
            </a>
        </li>
        
        
    </ul>

    <div class="social-link"><a href="#!" class="tooltipped" data-tooltip="邮件联系我: zy20081@126.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 314153271" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
</div>

            </div>
        </div>

        
    </nav>
</header>





<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/2.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        SSM 复习总结
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1,
    #articleContent h2,
    #articleContent h3,
    #articleContent h4,
    #articleContent h5,
    #articleContent h6 {
        padding-top: 76px;
        margin-top: -76px;
    }

    #articleContent h1 {
        line-height: 3.5rem;
    }

    #articleContent h2 {
        line-height: 3.2rem;
    }

    #articleContent h3 {
        line-height: 2.8rem;
    }

    #articleContent h4 {
        line-height: 2.5rem;
    }

    #articleContent h5 {
        line-height: 2.2rem;
    }

    #articleContent h6 {
        line-height: 1.9rem;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/框架/" target="_blank">
                    <span class="chip bg-color">框架</span>
                </a>
                
                <a href="/tags/SSM/" target="_blank">
                    <span class="chip bg-color">SSM</span>
                </a>
                
            </div>
            
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/JAVA/" class="post-category" target="_blank">
                        JAVA
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2019-01-24
                </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="Spring"><a href="#Spring" class="headerlink" title="Spring"></a>Spring</h2><p>spring 项目中对象容器，帮我们管理项目中的所有对象<br><br>Spring 就是一个大工厂，可以将所有对象创建和依赖关系维护，交给 Spring 管理<br>AOP 编程的支持<br><br>Spring 提供面向切面编程，可以方便的实现对程序进行权限拦截、运行监控等功能<br>声明式事务的支持<br><br>只需要通过配置就可以完成对事务的管理，而无需手动编程<br>方便程序的测试<br><br>Spring 对 Junit4 支持，可以通过注解方便的测试 Spring 程序<br>方便集成各种优秀框架<br><br>Spring 不排斥各种优秀的开源框架，其内部提供了对各种优秀框架（如：Struts、Hibernate、MyBatis、Quartz 等）的直接支持</p>
<h2 id="Spring搭建"><a href="#Spring搭建" class="headerlink" title="Spring搭建"></a>Spring搭建</h2><h3 id="1-下载Spring-的开发包"><a href="#1-下载Spring-的开发包" class="headerlink" title="1. 下载Spring 的开发包:"></a>1. 下载Spring 的开发包:</h3><pre><code>比如下spring-framework-4.2.4.RELEASE 包
</code></pre><h3 id="2-把需要的包放到lib下4-2-4个核心包-2个日志："><a href="#2-把需要的包放到lib下4-2-4个核心包-2个日志：" class="headerlink" title="2. 把需要的包放到lib下4+2 4个核心包+2个日志："></a>2. 把需要的包放到lib下4+2 4个核心包+2个日志：</h3><p> <img src="/2019/01/24/ssm-fu-xi-zong-jie/1.jpg" alt="图1"><br> 在WEB环境中使用Spring容器<br><br>    1&gt;导包 4+2+1(spring-web)<br><br>    2&gt;在web.xml中配置listener =&gt; ContextLoaderListener<br><br>        |-配置参数,指定spring配置路径 <br><br>    3&gt;在Action中,使用工具类获得容器.<br><br>        |-WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)</p>
<h3 id="3-创建一个对象"><a href="#3-创建一个对象" class="headerlink" title="3. 创建一个对象"></a>3. 创建一个对象</h3><pre class=" language-bash"><code class="language-bash"> package cn.itcast.bean<span class="token punctuation">;</span>
public class User <span class="token punctuation">{</span>
    public User<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            System.out.println<span class="token punctuation">(</span><span class="token string">"User对象空参构造方法!!!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    private String name<span class="token punctuation">;</span>
    private Integer age<span class="token punctuation">;</span>
    private Car car<span class="token punctuation">;</span>

    public User<span class="token punctuation">(</span>String name, Car car<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"User(String name, Car car)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        this.name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        this.car <span class="token operator">=</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public User<span class="token punctuation">(</span>Car car,String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"User(Car car,String name)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        this.name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        this.car <span class="token operator">=</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public User<span class="token punctuation">(</span>Integer name, Car car<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"User(Integer name, Car car)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        this.name <span class="token operator">=</span> name+<span class="token string">""</span><span class="token punctuation">;</span>
        this.car <span class="token operator">=</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public Car getCar<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setCar<span class="token punctuation">(</span>Car car<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.car <span class="token operator">=</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public String getName<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setName<span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public Integer getAge<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setAge<span class="token punctuation">(</span>Integer age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.age <span class="token operator">=</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public void init<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"我是初始化方法!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void destory<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"我是销毁方法!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public String toString<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"User [name="</span> + name + <span class="token string">", age="</span> + age + <span class="token string">", car="</span> + car + <span class="token string">"]"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<h3 id="4-创建配置文件applicationContext-xml-导入约束"><a href="#4-创建配置文件applicationContext-xml-导入约束" class="headerlink" title="4. 创建配置文件applicationContext.xml,导入约束"></a>4. 创建配置文件applicationContext.xml,导入约束</h3><pre><code>1.window-&gt;Preferences-&gt;搜cata，架&lt;br&gt;
2.点add-&gt;File System 选schema&gt;beans&gt;spring-beans-4.2.xsd架&lt;br&gt;
3.然后
![图2](2.jpg)
导入约束完成架&lt;br&gt;
4.xml文件中写beans,然后设计视图，右键 Edit Namespaces,然后Add,导入xsi,&lt;br&gt;
![图3](3.jpg)
![图4](4.jpg) 
</code></pre><p>   在点add，选择Specify New Namespace,Browse..&gt;<br>    Select XML Catalog entry选择<br>    <img src="/2019/01/24/ssm-fu-xi-zong-jie/5.jpg" alt="图5"><br>    <img src="/2019/01/24/ssm-fu-xi-zong-jie/6.jpg" alt="图6"><br>    <img src="/2019/01/24/ssm-fu-xi-zong-jie/7.jpg" alt="图7"> </p>
<pre class=" language-bash"><code class="language-bash">applicationContext.xml 导入成功后<span class="token operator">&lt;</span>会有提示的
引入约束:
spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configur
ation.html
<span class="token operator">&lt;</span>beans xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span>
 xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span>
 xsi:schemaLocation<span class="token operator">=</span><span class="token string">"
 http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 将User对象交给spring容器管理 --<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- Bean元素:使用该元素描述需要spring容器管理的对象
            class属性:被管理对象的完整类名.
            name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.  
                    可以重复.可以使用特殊字符.
            id属性: 与name属性一模一样. 
                    名称不可重复.不能使用特殊字符.
            结论: 尽量使用name属性.
            Scope:Prototype 多例，singletion 单例
      --<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 方式一--<span class="token operator">></span>      
<span class="token operator">&lt;</span>bean  name<span class="token operator">=</span><span class="token string">"user"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.bean.User"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>--静态工厂创建  调用UserFactory的createUser方法创建名为user2的对象.放入容器 --<span class="token operator">></span>     
<span class="token operator">&lt;</span>bean  name<span class="token operator">=</span><span class="token string">"user2"</span> 
        class<span class="token operator">=</span><span class="token string">"cn.itcast.b_create.UserFactory"</span> 
        factory-method<span class="token operator">=</span><span class="token string">"createUser"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 创建方式3:实例工厂创建 
         调用UserFactory对象的createUser2方法创建名为user3的对象.放入容器
         <span class="token operator">&lt;</span>bean  name<span class="token operator">=</span><span class="token string">"user3"</span> 
        factory-bean<span class="token operator">=</span><span class="token string">"userFactory"</span>
        factory-method<span class="token operator">=</span><span class="token string">"createUser2"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span>bean  name<span class="token operator">=</span><span class="token string">"userFactory"</span> 
        class<span class="token operator">=</span><span class="token string">"cn.itcast.b_create.UserFactory"</span>   <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
     --<span class="token operator">></span>
<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<h3 id="5-测试"><a href="#5-测试" class="headerlink" title="5. 测试"></a>5. 测试</h3><pre class=" language-bash"><code class="language-bash">public class Demo <span class="token punctuation">{</span>
    @Test
    public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        //1 创建容器对象
        ApplicationContext ac <span class="token operator">=</span> new ClassPathXmlApplicationContext<span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        /*//2 向容器<span class="token string">"要"</span>user对象
        User u <span class="token operator">=</span> <span class="token punctuation">(</span>User<span class="token punctuation">)</span> ac.getBean<span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //3 打印user对象
        System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>*/
    <span class="token punctuation">}</span>
        @Test
    public void fun2<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            //1 创建容器对象
            ApplicationContext ac <span class="token operator">=</span> new ClassPathXmlApplicationContext<span class="token punctuation">(</span><span class="token string">"cn/itcast/b_create/applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            //2 向容器<span class="token string">"要"</span>user对象
            User u <span class="token operator">=</span> <span class="token punctuation">(</span>User<span class="token punctuation">)</span> ac.getBean<span class="token punctuation">(</span><span class="token string">"user2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            //3 打印user对象
            System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //创建方式3:实例工厂
                @Test
                public void fun3<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    //1 创建容器对象
                    ApplicationContext ac <span class="token operator">=</span> new ClassPathXmlApplicationContext<span class="token punctuation">(</span><span class="token string">"cn/itcast/b_create/applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    //2 向容器<span class="token string">"要"</span>user对象
                    User u <span class="token operator">=</span> <span class="token punctuation">(</span>User<span class="token punctuation">)</span> ac.getBean<span class="token punctuation">(</span><span class="token string">"user3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    //3 打印user对象
                    System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

package cn.itcast.b_create<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.bean.User<span class="token punctuation">;</span>

public class UserFactory <span class="token punctuation">{</span>

    public static User createUser<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        System.out.println<span class="token punctuation">(</span><span class="token string">"静态工厂创建User"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    public  User createUser2<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        System.out.println<span class="token punctuation">(</span><span class="token string">"实例工厂创建User"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>


</code></pre>
<h2 id="Spring-依赖注入"><a href="#Spring-依赖注入" class="headerlink" title="Spring  依赖注入"></a>Spring  依赖注入</h2><p>IOC 反转控制：对象的创建以及依赖关系可以由spring完成创建以及注入<br><br>DI 依赖注入： 需要有 IOC 的环境,Spring 创建这个类的过程中,Spring 将类的依<br>赖的属性设置进去<br>注入方式：set方法注入，构造方法注入，字段注入，注入类型：值类型，引用类型</p>
<h3 id="注入"><a href="#注入" class="headerlink" title="注入"></a>注入</h3><pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> 
        xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span>
        xmlns:p<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/p"</span>
         xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "</span><span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- set方式注入: --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean  name<span class="token operator">=</span><span class="token string">"user"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.bean.User"</span> <span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>--值类型注入: 为User对象中名为name的属性注入tom作为值 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"name"</span> value<span class="token operator">=</span><span class="token string">"tom"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"age"</span>  value<span class="token operator">=</span><span class="token string">"18"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 引用类型注入: 为car属性注入下方配置的car对象 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"car"</span>  ref<span class="token operator">=</span><span class="token string">"car"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 将car对象配置到容器中 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"car"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.bean.Car"</span> <span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"name"</span> value<span class="token operator">=</span><span class="token string">"兰博基尼"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"color"</span> value<span class="token operator">=</span><span class="token string">"黄色"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- <span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span> --<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 构造函数注入 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"user2"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.bean.User"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- name属性: 构造函数的参数名 --<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- index属性: 构造函数的参数索引 --<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- type属性: 构造函数的参数类型--<span class="token operator">></span>
    <span class="token operator">&lt;</span>constructor-arg name<span class="token operator">=</span><span class="token string">"name"</span> index<span class="token operator">=</span><span class="token string">"0"</span> type<span class="token operator">=</span><span class="token string">"java.lang.Integer"</span> value<span class="token operator">=</span><span class="token string">"999"</span>  <span class="token operator">></span><span class="token operator">&lt;</span>/constructor-arg<span class="token operator">></span>
    <span class="token operator">&lt;</span>constructor-arg name<span class="token operator">=</span><span class="token string">"car"</span> ref<span class="token operator">=</span><span class="token string">"car"</span> index<span class="token operator">=</span><span class="token string">"1"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/constructor-arg<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">package cn.itcast.c_injection<span class="token punctuation">;</span>

<span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.ApplicationContext<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.support.ClassPathXmlApplicationContext<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.bean.User<span class="token punctuation">;</span>

public class Demo <span class="token punctuation">{</span>
    @Test
    public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        //1 创建容器对象
        ApplicationContext ac <span class="token operator">=</span> new ClassPathXmlApplicationContext<span class="token punctuation">(</span><span class="token string">"cn/itcast/c_injection/applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //2 向容器<span class="token string">"要"</span>user对象
        User u <span class="token operator">=</span> <span class="token punctuation">(</span>User<span class="token punctuation">)</span> ac.getBean<span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //3 打印user对象
        System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>
    @Test
    public void fun2<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        //1 创建容器对象
        ApplicationContext ac <span class="token operator">=</span> new ClassPathXmlApplicationContext<span class="token punctuation">(</span><span class="token string">"cn/itcast/c_injection/applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //2 向容器<span class="token string">"要"</span>user对象
        User u <span class="token operator">=</span> <span class="token punctuation">(</span>User<span class="token punctuation">)</span> ac.getBean<span class="token punctuation">(</span><span class="token string">"user2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //3 打印user对象
        System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>
</code></pre>
<h2 id="Spring-注解配置"><a href="#Spring-注解配置" class="headerlink" title="Spring  注解配置"></a>Spring  注解配置</h2><p>导入新的约束 spring-context-4.2.xsd，一样加xml命名空间<br> <img src="/2019/01/24/ssm-fu-xi-zong-jie/8.jpg" alt="图8"></p>
<pre class=" language-bash"><code class="language-bash"> applicationContext.xml
 <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "</span><span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定扫描cn.itcast.bean报下的所有类中的注解.
     注意:扫描包时.会扫描指定报下的所有子孙包
 --<span class="token operator">></span>
<span class="token operator">&lt;</span>context:component-scan base-package<span class="token operator">=</span><span class="token string">"cn.itcast.bean"</span><span class="token operator">></span><span class="token operator">&lt;</span>/context:component-scan<span class="token operator">></span>

<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"car2"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.bean.Car"</span>  <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"name"</span> value<span class="token operator">=</span><span class="token string">"布加迪威龙"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"color"</span> value<span class="token operator">=</span><span class="token string">"black"</span>  <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> User.java
 package cn.itcast.bean<span class="token punctuation">;</span>

<span class="token function">import</span> javax.annotation.PostConstruct<span class="token punctuation">;</span>
<span class="token function">import</span> javax.annotation.PreDestroy<span class="token punctuation">;</span>
<span class="token function">import</span> javax.annotation.Resource<span class="token punctuation">;</span>
<span class="token function">import</span> javax.xml.ws.RespectBinding<span class="token punctuation">;</span>

<span class="token function">import</span> org.springframework.beans.factory.annotation.Autowired<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.beans.factory.annotation.Qualifier<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.beans.factory.annotation.Value<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.annotation.Scope<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.stereotype.Component<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.stereotype.Controller<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.stereotype.Repository<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.stereotype.Service<span class="token punctuation">;</span>

//<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"user"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.bean.User"</span>  /<span class="token operator">></span>
//@Component<span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span>
//    @Service<span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span> // service层
//    @Controller<span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span> // web层
    @Repository<span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span>// dao层
//指定对象的作用范围
@Scope<span class="token punctuation">(</span>scopeName<span class="token operator">=</span><span class="token string">"singleton"</span><span class="token punctuation">)</span>
public class User <span class="token punctuation">{</span>
    private String name<span class="token punctuation">;</span>
    @Value<span class="token punctuation">(</span><span class="token string">"18"</span><span class="token punctuation">)</span>
    private Integer age<span class="token punctuation">;</span>

    //@Autowired //自动装配
    //问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.
    //@Qualifier<span class="token punctuation">(</span><span class="token string">"car2"</span><span class="token punctuation">)</span>//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象
    @Resource<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"car"</span><span class="token punctuation">)</span>//手动注入,指定注入哪个名称的对象
    private Car car<span class="token punctuation">;</span>

    public Car getCar<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setCar<span class="token punctuation">(</span>Car car<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.car <span class="token operator">=</span> car<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public String getName<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Value<span class="token punctuation">(</span><span class="token string">"tom"</span><span class="token punctuation">)</span>    
    public void setName<span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public Integer getAge<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setAge<span class="token punctuation">(</span>Integer age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.age <span class="token operator">=</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @PostConstruct //在对象被创建后调用.init-method
    public void init<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"我是初始化方法!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @PreDestroy //在销毁之前调用.destory-method
    public void destory<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"我是销毁方法!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public String toString<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">"User [name="</span> + name + <span class="token string">", age="</span> + age + <span class="token string">", car="</span> + car + <span class="token string">"]"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<h2 id="Spring-junit测试"><a href="#Spring-junit测试" class="headerlink" title="Spring  junit测试"></a>Spring  junit测试</h2><p>加入spring-test包：<br> <img src="/2019/01/24/ssm-fu-xi-zong-jie/9.jpg" alt="图9"></p>
<pre class=" language-bash"><code class="language-bash"> package cn.itcast.b_test<span class="token punctuation">;</span>

<span class="token function">import</span> javax.annotation.Resource<span class="token punctuation">;</span>

<span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
<span class="token function">import</span> org.junit.runner.RunWith<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.ApplicationContext<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.support.ClassPathXmlApplicationContext<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.test.context.ContextConfiguration<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.bean.User<span class="token punctuation">;</span>
//帮我们创建容器
@RunWith<span class="token punctuation">(</span>SpringJUnit4ClassRunner.class<span class="token punctuation">)</span>
//指定创建容器时使用哪个配置文件,要在src下面
@ContextConfiguration<span class="token punctuation">(</span><span class="token string">"classpath:applicationContext.xml"</span><span class="token punctuation">)</span>
public class Demo <span class="token punctuation">{</span>
    //将名为user的对象注入到u变量中
    @Resource<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"user"</span><span class="token punctuation">)</span>
    private User u<span class="token punctuation">;</span>

    @Test
    public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    @Test
    public void fun2<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    @Test
    public void fun3<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        System.out.println<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="Spring-AOP切面编程"><a href="#Spring-AOP切面编程" class="headerlink" title="Spring AOP切面编程"></a>Spring AOP切面编程</h2><h3 id="1-导包-4-2-2-2"><a href="#1-导包-4-2-2-2" class="headerlink" title="1.导包 4+2+2+2"></a>1.导包 4+2+2+2</h3><p> 前面一样，后面的包：<br> spring的aop包：<br><br>                      spring-aspects-4.2.4.RELEASE.jar<br><br>                     spring-aop-4.2.4.RELEASE.jar<br><br> spring需要第三方aop包：<br><br>    com.springsource.org.aopalliance-1.0.0.jar<br><br>    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar </p>
<p><img src="/2019/01/24/ssm-fu-xi-zong-jie/10.jpg" alt="图10"></p>
<h3 id="2-准备目标对象"><a href="#2-准备目标对象" class="headerlink" title="2.准备目标对象"></a>2.准备目标对象</h3><pre class=" language-bash"><code class="language-bash">package cn.itcast.service<span class="token punctuation">;</span>

public class UserServiceImpl implements UserService <span class="token punctuation">{</span>
    @Override
    public void save<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"保存用户!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //int i <span class="token operator">=</span> 1/0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public void delete<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"删除用户!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public void update<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"更新用户!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public void find<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"查找用户!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="3-准备通知"><a href="#3-准备通知" class="headerlink" title="3.准备通知"></a>3.准备通知</h3><pre class=" language-bash"><code class="language-bash"> package cn.itcast.d_springaop<span class="token punctuation">;</span>

<span class="token function">import</span> org.aspectj.lang.ProceedingJoinPoint<span class="token punctuation">;</span>

//通知类
public class MyAdvice <span class="token punctuation">{</span>

    //前置通知    
//        <span class="token operator">|</span>-目标方法运行之前调用
    //后置通知<span class="token punctuation">(</span>如果出现异常不会调用<span class="token punctuation">)</span>
//        <span class="token operator">|</span>-在目标方法运行之后调用
    //环绕通知
//        <span class="token operator">|</span>-在目标方法之前和之后都调用
    //异常拦截通知
//        <span class="token operator">|</span>-如果出现异常,就会调用
    //后置通知<span class="token punctuation">(</span>无论是否出现 异常都会调用<span class="token punctuation">)</span>
//        <span class="token operator">|</span>-在目标方法运行之后调用
//----------------------------------------------------------------
    //前置通知
    public void before<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是前置通知!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //后置通知
    public void afterReturning<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是后置通知(如果出现异常不会调用)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //环绕通知
    public Object around<span class="token punctuation">(</span>ProceedingJoinPoint pjp<span class="token punctuation">)</span> throws Throwable <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是环绕通知之前的部分!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Object proceed <span class="token operator">=</span> pjp.proceed<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>//调用目标方法
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是环绕通知之后的部分!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> proceed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //异常通知
    public void afterException<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"出事啦!出现异常了!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //后置通知
    public void after<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是后置通知(出现异常也会调用)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="4-配置进行织入-将通知织入目标对象中"><a href="#4-配置进行织入-将通知织入目标对象中" class="headerlink" title="4.配置进行织入,将通知织入目标对象中"></a>4.配置进行织入,将通知织入目标对象中</h3><pre class=" language-bash"><code class="language-bash"> applicationContext.xml 
 导入新的约束 spring-aop-4.2.xsd，一样加xml命名空间
 <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xmlns:aop<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/aop"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "</span><span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 准备工作: 导入aop<span class="token punctuation">(</span>约束<span class="token punctuation">)</span>命名空间 --<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 1.配置目标对象 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"userService"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.service.UserServiceImpl"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 2.配置通知对象 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"myAdvice"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.d_springaop.MyAdvice"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 3.配置将通知织入目标对象 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>aop:config<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置切入点 
            public void cn.itcast.service.UserServiceImpl.save<span class="token punctuation">(</span><span class="token punctuation">)</span> 
            void cn.itcast.service.UserServiceImpl.save<span class="token punctuation">(</span><span class="token punctuation">)</span>
            * cn.itcast.service.UserServiceImpl.save<span class="token punctuation">(</span><span class="token punctuation">)</span>
            * cn.itcast.service.UserServiceImpl.*<span class="token punctuation">(</span><span class="token punctuation">)</span>

            * cn.itcast.service.*ServiceImpl.*<span class="token punctuation">(</span><span class="token punctuation">..</span><span class="token punctuation">)</span>
            * cn.itcast.service<span class="token punctuation">..</span>*ServiceImpl.*<span class="token punctuation">(</span><span class="token punctuation">..</span><span class="token punctuation">)</span>
        --<span class="token operator">></span>
        <span class="token operator">&lt;</span>aop:pointcut expression<span class="token operator">=</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span> id<span class="token operator">=</span><span class="token string">"pc"</span>/<span class="token operator">></span>
        <span class="token operator">&lt;</span>aop:aspect ref<span class="token operator">=</span><span class="token string">"myAdvice"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定名为before方法作为前置通知 --<span class="token operator">></span>
            <span class="token operator">&lt;</span>aop:before method<span class="token operator">=</span><span class="token string">"before"</span> pointcut-ref<span class="token operator">=</span><span class="token string">"pc"</span> /<span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 后置 --<span class="token operator">></span>
            <span class="token operator">&lt;</span>aop:after-returning method<span class="token operator">=</span><span class="token string">"afterReturning"</span> pointcut-ref<span class="token operator">=</span><span class="token string">"pc"</span> /<span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 环绕通知 --<span class="token operator">></span>
            <span class="token operator">&lt;</span>aop:around method<span class="token operator">=</span><span class="token string">"around"</span> pointcut-ref<span class="token operator">=</span><span class="token string">"pc"</span> /<span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 异常拦截通知 --<span class="token operator">></span>
            <span class="token operator">&lt;</span>aop:after-throwing method<span class="token operator">=</span><span class="token string">"afterException"</span> pointcut-ref<span class="token operator">=</span><span class="token string">"pc"</span>/<span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 后置 --<span class="token operator">></span>
            <span class="token operator">&lt;</span>aop:after method<span class="token operator">=</span><span class="token string">"after"</span> pointcut-ref<span class="token operator">=</span><span class="token string">"pc"</span>/<span class="token operator">></span>
        <span class="token operator">&lt;</span>/aop:aspect<span class="token operator">></span>
    <span class="token operator">&lt;</span>/aop:config<span class="token operator">></span>
<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
--测试
  package cn.itcast.d_springaop<span class="token punctuation">;</span>

  <span class="token function">import</span> javax.annotation.Resource<span class="token punctuation">;</span>

  <span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
  <span class="token function">import</span> org.junit.runner.RunWith<span class="token punctuation">;</span>
  <span class="token function">import</span> org.springframework.context.ApplicationContext<span class="token punctuation">;</span>
  <span class="token function">import</span> org.springframework.context.support.ClassPathXmlApplicationContext<span class="token punctuation">;</span>
  <span class="token function">import</span> org.springframework.test.context.ContextConfiguration<span class="token punctuation">;</span>
  <span class="token function">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner<span class="token punctuation">;</span>

  <span class="token function">import</span> cn.itcast.bean.User<span class="token punctuation">;</span>
  <span class="token function">import</span> cn.itcast.service.UserService<span class="token punctuation">;</span>
  @RunWith<span class="token punctuation">(</span>SpringJUnit4ClassRunner.class<span class="token punctuation">)</span>
  @ContextConfiguration<span class="token punctuation">(</span><span class="token string">"classpath:cn/itcast/d_springaop/applicationContext.xml"</span><span class="token punctuation">)</span>
  public class Demo <span class="token punctuation">{</span>
      @Resource<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"userService"</span><span class="token punctuation">)</span>
      private UserService us<span class="token punctuation">;</span>

      @Test
      public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
          us.save<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>

  <span class="token punctuation">}</span>
</code></pre>
<h3 id="用注解的写法"><a href="#用注解的写法" class="headerlink" title="用注解的写法"></a>用注解的写法</h3><pre class=" language-bash"><code class="language-bash">  applicationContext.xml 
 <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xmlns:aop<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/aop"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "</span><span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 准备工作: 导入aop<span class="token punctuation">(</span>约束<span class="token punctuation">)</span>命名空间 --<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 1.配置目标对象 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"userService"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.service.UserServiceImpl"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 2.配置通知对象 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"myAdvice"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.e_annotationaop.MyAdvice"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 3.开启使用注解完成织入 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>aop:aspectj-autoproxy<span class="token operator">></span><span class="token operator">&lt;</span>/aop:aspectj-autoproxy<span class="token operator">></span>

<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> package cn.itcast.e_annotationaop<span class="token punctuation">;</span>

<span class="token function">import</span> org.aspectj.lang.ProceedingJoinPoint<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.After<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.AfterReturning<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.AfterThrowing<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.Around<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.Aspect<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.Before<span class="token punctuation">;</span>
<span class="token function">import</span> org.aspectj.lang.annotation.Pointcut<span class="token punctuation">;</span>

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice <span class="token punctuation">{</span>
    @Pointcut<span class="token punctuation">(</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span><span class="token punctuation">)</span>
    public void pc<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    //前置通知
    //指定该方法是前置通知,并制定切入点
    @Before<span class="token punctuation">(</span><span class="token string">"MyAdvice.pc()"</span><span class="token punctuation">)</span>
    public void before<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是前置通知!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //后置通知
    @AfterReturning<span class="token punctuation">(</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span><span class="token punctuation">)</span>
    public void afterReturning<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是后置通知(如果出现异常不会调用)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //环绕通知
    @Around<span class="token punctuation">(</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span><span class="token punctuation">)</span>
    public Object around<span class="token punctuation">(</span>ProceedingJoinPoint pjp<span class="token punctuation">)</span> throws Throwable <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是环绕通知之前的部分!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Object proceed <span class="token operator">=</span> pjp.proceed<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>//调用目标方法
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是环绕通知之后的部分!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> proceed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //异常通知
    @AfterThrowing<span class="token punctuation">(</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span><span class="token punctuation">)</span>
    public void afterException<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"出事啦!出现异常了!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    //后置通知
    @After<span class="token punctuation">(</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span><span class="token punctuation">)</span>
    public void after<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span><span class="token string">"这是后置通知(出现异常也会调用)!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
测试还是上面一样的测
</code></pre>
<h2 id="spring整合JDBC"><a href="#spring整合JDBC" class="headerlink" title="spring整合JDBC"></a>spring整合JDBC</h2><h3 id="导包"><a href="#导包" class="headerlink" title="导包"></a>导包</h3><p>4+2+spring-test，spring-aop，junit4类库<br>c3p0连接池，JDBC驱动，spring-jdbc，spring-tx事务</p>
<p><img src="/2019/01/24/ssm-fu-xi-zong-jie/11.jpg" alt="图11"></p>
<h3 id="Dao"><a href="#Dao" class="headerlink" title="Dao"></a>Dao</h3><pre class=" language-bash"><code class="language-bash">package cn.itcast.a_jdbctemplate<span class="token punctuation">;</span>

<span class="token function">import</span> java.sql.ResultSet<span class="token punctuation">;</span>
<span class="token function">import</span> java.sql.SQLException<span class="token punctuation">;</span>
<span class="token function">import</span> java.util.List<span class="token punctuation">;</span>

<span class="token function">import</span> org.springframework.jdbc.core.JdbcTemplate<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.jdbc.core.RowMapper<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.jdbc.core.support.JdbcDaoSupport<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.bean.User<span class="token punctuation">;</span>
//使用JDBC模板实现增删改查
public class UserDaoImpl extends JdbcDaoSupport implements UserDao <span class="token punctuation">{</span>
    @Override
    public void save<span class="token punctuation">(</span>User u<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        String sql <span class="token operator">=</span> <span class="token string">"insert into t_user values(null,?) "</span><span class="token punctuation">;</span>
        super.getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.update<span class="token punctuation">(</span>sql, u.getName<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public void delete<span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        String sql <span class="token operator">=</span> <span class="token string">"delete from t_user where id = ? "</span><span class="token punctuation">;</span>
        super.getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.update<span class="token punctuation">(</span>sql,id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public void update<span class="token punctuation">(</span>User u<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        String sql <span class="token operator">=</span> <span class="token string">"update  t_user set name = ? where id=? "</span><span class="token punctuation">;</span>
        super.getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.update<span class="token punctuation">(</span>sql, u.getName<span class="token punctuation">(</span><span class="token punctuation">)</span>,u.getId<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public User getById<span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        String sql <span class="token operator">=</span> <span class="token string">"select * from t_user where id = ? "</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> super.getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.queryForObject<span class="token punctuation">(</span>sql,new RowMapper<span class="token operator">&lt;</span>User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            @Override
            public User mapRow<span class="token punctuation">(</span>ResultSet rs, int arg1<span class="token punctuation">)</span> throws SQLException <span class="token punctuation">{</span>
                User u <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                u.setId<span class="token punctuation">(</span>rs.getInt<span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
                u.setName<span class="token punctuation">(</span>rs.getString<span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> u<span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">}</span>, id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>
    @Override
    public int getTotalCount<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        String sql <span class="token operator">=</span> <span class="token string">"select count(*) from t_user  "</span><span class="token punctuation">;</span>
        Integer count <span class="token operator">=</span> super.getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.queryForObject<span class="token punctuation">(</span>sql, Integer.class<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> count<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Override
    public List<span class="token operator">&lt;</span>User<span class="token operator">></span> getAll<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        String sql <span class="token operator">=</span> <span class="token string">"select * from t_user  "</span><span class="token punctuation">;</span>
        List<span class="token operator">&lt;</span>User<span class="token operator">></span> list <span class="token operator">=</span> super.getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.query<span class="token punctuation">(</span>sql, new RowMapper<span class="token operator">&lt;</span>User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            @Override
            public User mapRow<span class="token punctuation">(</span>ResultSet rs, int arg1<span class="token punctuation">)</span> throws SQLException <span class="token punctuation">{</span>
                User u <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                u.setId<span class="token punctuation">(</span>rs.getInt<span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
                u.setName<span class="token punctuation">(</span>rs.getString<span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> u<span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> list<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="applicationContext-xml配置"><a href="#applicationContext-xml配置" class="headerlink" title="applicationContext.xml配置"></a>applicationContext.xml配置</h3><pre class=" language-bash"><code class="language-bash"> <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "</span><span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定spring读取db.properties配置 --<span class="token operator">></span>
<span class="token operator">&lt;</span>context:property-placeholder location<span class="token operator">=</span><span class="token string">"classpath:db.properties"</span>  /<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 1.将连接池放入spring容器 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"dataSource"</span> class<span class="token operator">=</span><span class="token string">"com.mchange.v2.c3p0.ComboPooledDataSource"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"jdbcUrl"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.jdbcUrl}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"driverClass"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.driverClass}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"user"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.user}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"password"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.password}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>


<span class="token operator">&lt;</span><span class="token operator">!</span>-- 2.将JDBCTemplate放入spring容器 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"jdbcTemplate"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.jdbc.core.JdbcTemplate"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 3.将UserDao放入spring容器 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"userDao"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.a_jdbctemplate.UserDaoImpl"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"jt"</span> ref<span class="token operator">=</span><span class="token string">"jdbcTemplate"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span> --<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span>/beans<span class="token operator">></span>

db.properties
jdbc.jdbcUrl<span class="token operator">=</span>jdbc:mysql:///hibernate_32
jdbc.driverClass<span class="token operator">=</span>com.mysql.jdbc.Driver
jdbc.user<span class="token operator">=</span>root
jdbc.password<span class="token operator">=</span>1234
</code></pre>
<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><pre class=" language-bash"><code class="language-bash"> package cn.itcast.a_jdbctemplate<span class="token punctuation">;</span>

<span class="token function">import</span> java.beans.PropertyVetoException<span class="token punctuation">;</span>

<span class="token function">import</span> javax.annotation.Resource<span class="token punctuation">;</span>

<span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
<span class="token function">import</span> org.junit.runner.RunWith<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.jdbc.core.JdbcTemplate<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.test.context.ContextConfiguration<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner<span class="token punctuation">;</span>

<span class="token function">import</span> com.mchange.v2.c3p0.ComboPooledDataSource<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.bean.User<span class="token punctuation">;</span>

//演示JDBC模板
@RunWith<span class="token punctuation">(</span>SpringJUnit4ClassRunner.class<span class="token punctuation">)</span>
@ContextConfiguration<span class="token punctuation">(</span><span class="token string">"classpath:applicationContext.xml"</span><span class="token punctuation">)</span>
public class Demo <span class="token punctuation">{</span>
        @Resource<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"userDao"</span><span class="token punctuation">)</span>
    private UserDao ud<span class="token punctuation">;</span>

    @Test
    public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>

        //0 准备连接池
        ComboPooledDataSource dataSource <span class="token operator">=</span> new ComboPooledDataSource<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dataSource.setDriverClass<span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dataSource.setJdbcUrl<span class="token punctuation">(</span><span class="token string">"jdbc:mysql:///hibernate_32"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dataSource.setUser<span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dataSource.setPassword<span class="token punctuation">(</span><span class="token string">"1234"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //1 创建JDBC模板对象
        JdbcTemplate jt <span class="token operator">=</span> new JdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        jt.setDataSource<span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //2 书写sql,并执行
        String sql <span class="token operator">=</span> <span class="token string">"insert into t_user values(null,'rose') "</span><span class="token punctuation">;</span>
        jt.update<span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    @Test
    public void fun2<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        User u <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        u.setName<span class="token punctuation">(</span><span class="token string">"tom"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ud.save<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Test
    public void fun3<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        User u <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        u.setId<span class="token punctuation">(</span>2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        u.setName<span class="token punctuation">(</span><span class="token string">"jack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ud.update<span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    @Test
    public void fun4<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        ud.delete<span class="token punctuation">(</span>2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Test
    public void fun5<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span>ud.getTotalCount<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Test
    public void fun6<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span>ud.getById<span class="token punctuation">(</span>1<span class="token punctuation">))</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Test
    public void fun7<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span>ud.getAll<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

</code></pre>
<h2 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h2><h2 id="导入新的约束"><a href="#导入新的约束" class="headerlink" title="导入新的约束"></a>导入新的约束</h2><p>spring-tx-4.2.4.RELEASE</p>
<p> <img src="/2019/01/24/ssm-fu-xi-zong-jie/12.jpg" alt="图12"></p>
<h2 id="事务配置"><a href="#事务配置" class="headerlink" title="事务配置"></a>事务配置</h2><pre class=" language-bash"><code class="language-bash"> applicationContext.xml
 <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xmlns:aop<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/aop"</span> xmlns:tx<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/tx"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "</span><span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定spring读取db.properties配置 --<span class="token operator">></span>
<span class="token operator">&lt;</span>context:property-placeholder location<span class="token operator">=</span><span class="token string">"classpath:db.properties"</span>  /<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"transactionManager"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 事务模板对象 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"transactionTemplate"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.transaction.support.TransactionTemplate"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"transactionManager"</span> ref<span class="token operator">=</span><span class="token string">"transactionManager"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置事务通知 --<span class="token operator">></span>
<span class="token operator">&lt;</span>tx:advice id<span class="token operator">=</span><span class="token string">"txAdvice"</span> transaction-manager<span class="token operator">=</span><span class="token string">"transactionManager"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>tx:attributes<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 以方法为单位,指定方法应用什么事务属性
            isolation:隔离级别
            propagation:传播行为
            read-only:是否只读
         --<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"save*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"persist*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"update*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"modify*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"delete*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"remove*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"get*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"true"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"find*"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"true"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>tx:method name<span class="token operator">=</span><span class="token string">"transfer"</span> isolation<span class="token operator">=</span><span class="token string">"REPEATABLE_READ"</span> propagation<span class="token operator">=</span><span class="token string">"REQUIRED"</span> read-only<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/tx:attributes<span class="token operator">></span>
<span class="token operator">&lt;</span>/tx:advice<span class="token operator">></span>


<span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置织入 --<span class="token operator">></span>
<span class="token operator">&lt;</span>aop:config  <span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置切点表达式 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>aop:pointcut expression<span class="token operator">=</span><span class="token string">"execution(* cn.itcast.service.*ServiceImpl.*(..))"</span> id<span class="token operator">=</span><span class="token string">"txPc"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置切面 <span class="token keyword">:</span> 通知+切点
             advice-ref:通知的名称
             pointcut-ref:切点的名称
     --<span class="token operator">></span>
    <span class="token operator">&lt;</span>aop:advisor advice-ref<span class="token operator">=</span><span class="token string">"txAdvice"</span> pointcut-ref<span class="token operator">=</span><span class="token string">"txPc"</span> /<span class="token operator">></span>
<span class="token operator">&lt;</span>/aop:config<span class="token operator">></span>


<span class="token operator">&lt;</span><span class="token operator">!</span>-- 1.将连接池 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"dataSource"</span> class<span class="token operator">=</span><span class="token string">"com.mchange.v2.c3p0.ComboPooledDataSource"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"jdbcUrl"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.jdbcUrl}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"driverClass"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.driverClass}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"user"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.user}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"password"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.password}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>



<span class="token operator">&lt;</span><span class="token operator">!</span>-- 2.Dao--<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"accountDao"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.dao.AccountDaoImpl"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 3.Service--<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"accountService"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.service.AccountServiceImpl"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"ad"</span> ref<span class="token operator">=</span><span class="token string">"accountDao"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"tt"</span> ref<span class="token operator">=</span><span class="token string">"transactionTemplate"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>  

<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> AccountDaoImpl.java
 package cn.itcast.dao<span class="token punctuation">;</span>

<span class="token function">import</span> org.springframework.jdbc.core.support.JdbcDaoSupport<span class="token punctuation">;</span>

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  <span class="token punctuation">{</span>

    @Override
    public void increaseMoney<span class="token punctuation">(</span>Integer id, Double money<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.update<span class="token punctuation">(</span><span class="token string">"update t_account set money = money+? where id = ? "</span>, money,id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    @Override
    public void decreaseMoney<span class="token punctuation">(</span>Integer id, Double money<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        getJdbcTemplate<span class="token punctuation">(</span><span class="token punctuation">)</span>.update<span class="token punctuation">(</span><span class="token string">"update t_account set money = money-? where id = ? "</span>, money,id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<p>测试</p>
<pre class=" language-bash"><code class="language-bash"> package cn.itcast.tx<span class="token punctuation">;</span>

<span class="token function">import</span> javax.annotation.Resource<span class="token punctuation">;</span>

<span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
<span class="token function">import</span> org.junit.runner.RunWith<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.test.context.ContextConfiguration<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.service.AccountService<span class="token punctuation">;</span>

@RunWith<span class="token punctuation">(</span>SpringJUnit4ClassRunner.class<span class="token punctuation">)</span>
@ContextConfiguration<span class="token punctuation">(</span><span class="token string">"classpath:applicationContext.xml"</span><span class="token punctuation">)</span>
public class Demo <span class="token punctuation">{</span>
        @Resource<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"accountService"</span><span class="token punctuation">)</span>
    private AccountService as<span class="token punctuation">;</span>

    @Test
    public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        as.transfer<span class="token punctuation">(</span>1, 2, 100d<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre>
<h2 id="注解配置"><a href="#注解配置" class="headerlink" title="注解配置"></a>注解配置</h2><pre class=" language-bash"><code class="language-bash"> <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xmlns:aop<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/aop"</span> xmlns:tx<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/tx"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "</span><span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定spring读取db.properties配置 --<span class="token operator">></span>
<span class="token operator">&lt;</span>context:property-placeholder location<span class="token operator">=</span><span class="token string">"classpath:db.properties"</span>  /<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"transactionManager"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 事务模板对象 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"transactionTemplate"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.transaction.support.TransactionTemplate"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"transactionManager"</span> ref<span class="token operator">=</span><span class="token string">"transactionManager"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 开启使用注解管理aop事务 --<span class="token operator">></span>
<span class="token operator">&lt;</span>tx:annotation-driven/<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- 1.将连接池 --<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"dataSource"</span> class<span class="token operator">=</span><span class="token string">"com.mchange.v2.c3p0.ComboPooledDataSource"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"jdbcUrl"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.jdbcUrl}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"driverClass"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.driverClass}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"user"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.user}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"password"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.password}</span>"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>



<span class="token operator">&lt;</span><span class="token operator">!</span>-- 2.Dao--<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"accountDao"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.dao.AccountDaoImpl"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 3.Service--<span class="token operator">></span>
<span class="token operator">&lt;</span>bean name<span class="token operator">=</span><span class="token string">"accountService"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.service.AccountServiceImpl"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"ad"</span> ref<span class="token operator">=</span><span class="token string">"accountDao"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"tt"</span> ref<span class="token operator">=</span><span class="token string">"transactionTemplate"</span> <span class="token operator">></span><span class="token operator">&lt;</span>/property<span class="token operator">></span>
<span class="token operator">&lt;</span>/bean<span class="token operator">></span>  

<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">  AccountServiceImpl.java
  package cn.itcast.service<span class="token punctuation">;</span>

<span class="token function">import</span> org.springframework.transaction.TransactionStatus<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.transaction.annotation.Isolation<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.transaction.annotation.Propagation<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.transaction.annotation.Transactional<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.transaction.support.TransactionCallback<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.transaction.support.TransactionCallbackWithoutResult<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.transaction.support.TransactionTemplate<span class="token punctuation">;</span>

<span class="token function">import</span> cn.itcast.dao.AccountDao<span class="token punctuation">;</span>

@Transactional<span class="token punctuation">(</span>isolation<span class="token operator">=</span>Isolation.REPEATABLE_READ,propagation<span class="token operator">=</span>Propagation.REQUIRED,readOnly<span class="token operator">=</span>true<span class="token punctuation">)</span>
public class AccountServiceImpl implements AccountService <span class="token punctuation">{</span>

    private AccountDao ad <span class="token punctuation">;</span>
    private TransactionTemplate tt<span class="token punctuation">;</span>

    @Override
    @Transactional<span class="token punctuation">(</span>isolation<span class="token operator">=</span>Isolation.REPEATABLE_READ,propagation<span class="token operator">=</span>Propagation.REQUIRED,readOnly<span class="token operator">=</span>false<span class="token punctuation">)</span>
    public void transfer<span class="token punctuation">(</span>final Integer from,final Integer to,final Double money<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                //减钱
                ad.decreaseMoney<span class="token punctuation">(</span>from, money<span class="token punctuation">)</span><span class="token punctuation">;</span>
                int i <span class="token operator">=</span> 1/0<span class="token punctuation">;</span>
                //加钱
                ad.increaseMoney<span class="token punctuation">(</span>to, money<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setAd<span class="token punctuation">(</span>AccountDao ad<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.ad <span class="token operator">=</span> ad<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setTt<span class="token punctuation">(</span>TransactionTemplate tt<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.tt <span class="token operator">=</span> tt<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="MyBatis"><a href="#MyBatis" class="headerlink" title="MyBatis"></a>MyBatis</h2><h3 id="导包-1"><a href="#导包-1" class="headerlink" title="导包"></a>导包</h3><p>下载mybatis<br>加入mybatis包和mysql驱动包</p>
<h3 id="xml-一般使用"><a href="#xml-一般使用" class="headerlink" title="xml 一般使用"></a>xml 一般使用</h3><pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span> ?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE mapper
PUBLIC <span class="token string">"-//mybatis.org//DTD Mapper 3.0//EN"</span>
<span class="token string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 写Sql语句   --<span class="token operator">></span>
<span class="token operator">&lt;</span>mapper namespace<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.mapper.UserMapper"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>sql id<span class="token operator">=</span><span class="token string">"selector"</span><span class="token operator">></span>
        <span class="token keyword">select</span> * from user
    <span class="token operator">&lt;</span>/sql<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 通过ID查询一个用户 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"findUserById"</span> parameterType<span class="token operator">=</span><span class="token string">"Integer"</span> resultType<span class="token operator">=</span><span class="token string">"User"</span><span class="token operator">></span>
        <span class="token keyword">select</span> * from user where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{v}</span>
    <span class="token operator">&lt;</span>/select<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- //根据用户名称模糊查询用户列表
    <span class="token comment" spellcheck="true">#{}    select * from user where id = ？    占位符  ? ==  '五'</span>
    $<span class="token punctuation">{</span><span class="token punctuation">}</span>    <span class="token keyword">select</span> * from user where username like <span class="token string">'%五%'</span>  字符串拼接  

     --<span class="token operator">></span>
    <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"findUserByUsername"</span> parameterType<span class="token operator">=</span><span class="token string">"String"</span> resultType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span><span class="token operator">></span>
        <span class="token keyword">select</span> * from user where username like <span class="token string">"%"</span>#<span class="token punctuation">{</span>haha<span class="token punctuation">}</span><span class="token string">"%"</span>
    <span class="token operator">&lt;</span>/select<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 添加用户 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>insert id<span class="token operator">=</span><span class="token string">"insertUser"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>selectKey keyProperty<span class="token operator">=</span><span class="token string">"id"</span> resultType<span class="token operator">=</span><span class="token string">"Integer"</span> order<span class="token operator">=</span><span class="token string">"AFTER"</span><span class="token operator">></span>
            <span class="token keyword">select</span> LAST_INSERT_ID<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token operator">&lt;</span>/selectKey<span class="token operator">></span>
        insert into user <span class="token punctuation">(</span>username,birthday,address,sex<span class="token punctuation">)</span> 
        values <span class="token punctuation">(</span><span class="token comment" spellcheck="true">#{username},#{birthday},#{address},#{sex})</span>
    <span class="token operator">&lt;</span>/insert<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 更新 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>update id<span class="token operator">=</span><span class="token string">"updateUserById"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span><span class="token operator">></span>
        update user 
        <span class="token keyword">set</span> username <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{username},sex = #{sex},birthday = #{birthday},address = #{address}</span>
        where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{id}</span>
    <span class="token operator">&lt;</span>/update<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 删除 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>delete id<span class="token operator">=</span><span class="token string">"deleteUserById"</span> parameterType<span class="token operator">=</span><span class="token string">"Integer"</span><span class="token operator">></span>
        delete from user 
        where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{vvvvv}</span>
    <span class="token operator">&lt;</span>/delete<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 根据用户名模糊查询 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"findUserByQueryVo"</span> parameterType<span class="token operator">=</span><span class="token string">"QueryVo"</span> resultType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span><span class="token operator">></span>
        <span class="token keyword">select</span> * from user where username like <span class="token string">"%"</span>#<span class="token punctuation">{</span>user.username<span class="token punctuation">}</span><span class="token string">"%"</span>
    <span class="token operator">&lt;</span>/select<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 
     --<span class="token operator">></span>
     <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"countUser"</span> resultType<span class="token operator">=</span><span class="token string">"Integer"</span><span class="token operator">></span>
         <span class="token keyword">select</span> count<span class="token punctuation">(</span>1<span class="token punctuation">)</span> from user
     <span class="token operator">&lt;</span>/select<span class="token operator">></span>


     <span class="token operator">&lt;</span><span class="token operator">!</span>--    根据性别和名字查询用户  where 可以去掉第一个前ANd   --<span class="token operator">></span>
     <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"selectUserBySexAndUsername"</span> parameterType<span class="token operator">=</span><span class="token string">"User"</span> resultType<span class="token operator">=</span><span class="token string">"User"</span><span class="token operator">></span>
         <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"selector"</span>/<span class="token operator">></span>
         <span class="token operator">&lt;</span>where<span class="token operator">></span>
             <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"sex != null and sex != ''"</span><span class="token operator">></span>
                  and sex <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{sex} </span>
             <span class="token operator">&lt;</span>/if<span class="token operator">></span>
             <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"username != null and username != ''"</span><span class="token operator">></span>
                  and username <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{username}</span>
             <span class="token operator">&lt;</span>/if<span class="token operator">></span>
         <span class="token operator">&lt;</span>/where<span class="token operator">></span>
     <span class="token operator">&lt;</span>/select<span class="token operator">></span>

     <span class="token operator">&lt;</span><span class="token operator">!</span>-- 多个ID <span class="token punctuation">(</span>1,2,3<span class="token punctuation">)</span>--<span class="token operator">></span>
     <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"selectUserByIds"</span> parameterType<span class="token operator">=</span><span class="token string">"QueryVo"</span> resultType<span class="token operator">=</span><span class="token string">"User"</span><span class="token operator">></span>
         <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"selector"</span>/<span class="token operator">></span>
         <span class="token operator">&lt;</span>where<span class="token operator">></span>
             <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"list"</span> item<span class="token operator">=</span><span class="token string">"id"</span> separator<span class="token operator">=</span><span class="token string">","</span> open<span class="token operator">=</span><span class="token string">"id in ("</span> close<span class="token operator">=</span><span class="token string">")"</span><span class="token operator">></span>
                 <span class="token comment" spellcheck="true">#{id}</span>
             <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
         <span class="token operator">&lt;</span>/where<span class="token operator">></span>
     <span class="token operator">&lt;</span>/select<span class="token operator">></span>
<span class="token operator">&lt;</span>/mapper<span class="token operator">></span>
</code></pre>
<h2 id="mybatis-spring整合"><a href="#mybatis-spring整合" class="headerlink" title="mybatis+spring整合"></a>mybatis+spring整合</h2><p><img src="/2019/01/24/ssm-fu-xi-zong-jie/13.jpg" alt="图13"></p>
<h3 id="sqlMapConfig-xml"><a href="#sqlMapConfig-xml" class="headerlink" title="sqlMapConfig.xml"></a>sqlMapConfig.xml</h3><pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span> ?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE configuration
PUBLIC <span class="token string">"-//mybatis.org//DTD Config 3.0//EN"</span>
<span class="token string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>configuration<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 设置别名 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>typeAliases<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 2. 指定扫描包，会把包内所有的类都设置别名，别名的名称就是类名，大小写不敏感 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/typeAliases<span class="token operator">></span>

    <span class="token operator">&lt;</span>mappers<span class="token operator">></span>
        <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.mapper"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>/mappers<span class="token operator">></span>
<span class="token operator">&lt;</span>/configuration<span class="token operator">></span>
</code></pre>
<h3 id="applicationContext-xml"><a href="#applicationContext-xml" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h3><pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span>
    xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xmlns:p<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/p"</span>
    xmlns:aop<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/aop"</span> xmlns:tx<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/tx"</span>
    xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span>
    xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"</span><span class="token operator">></span>


    <span class="token operator">&lt;</span>context:property-placeholder location<span class="token operator">=</span><span class="token string">"classpath:db.properties"</span>/<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 数据库连接池 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"dataSource"</span> class<span class="token operator">=</span><span class="token string">"org.apache.commons.dbcp.BasicDataSource"</span>
        destroy-method<span class="token operator">=</span><span class="token string">"close"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"driverClassName"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.driver}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"url"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.url}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"username"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.username}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"password"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.password}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxActive"</span> value<span class="token operator">=</span><span class="token string">"10"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxIdle"</span> value<span class="token operator">=</span><span class="token string">"5"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- Mybatis的工厂 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"sqlSessionFactoryBean"</span> class<span class="token operator">=</span><span class="token string">"org.mybatis.spring.SqlSessionFactoryBean"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span>/<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 核心配置文件的位置 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"configLocation"</span> value<span class="token operator">=</span><span class="token string">"classpath:sqlMapConfig.xml"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>



    <span class="token operator">&lt;</span><span class="token operator">!</span>-- Mapper动态代理开发   扫描 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 基本包 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"basePackage"</span> value<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.mapper"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>
<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> db.properties
jdbc.driver<span class="token operator">=</span>com.mysql.jdbc.Driver
jdbc.url<span class="token operator">=</span>jdbc:mysql://localhost:3306/mybatis?characterEncoding<span class="token operator">=</span>utf-8
jdbc.username<span class="token operator">=</span>root
jdbc.password<span class="token operator">=</span>root
</code></pre>
<pre class=" language-bash"><code class="language-bash">log4j.properties
 <span class="token comment" spellcheck="true"># Global logging configuration</span>
log4j.rootLogger<span class="token operator">=</span>DEBUG, stdout
<span class="token comment" spellcheck="true"># Console output...</span>
log4j.appender.stdout<span class="token operator">=</span>org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout<span class="token operator">=</span>org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern<span class="token operator">=</span>%5p <span class="token punctuation">[</span>%t<span class="token punctuation">]</span> - %m%n
</code></pre>
<h3 id="Pojo"><a href="#Pojo" class="headerlink" title="Pojo"></a>Pojo</h3><pre class=" language-bash"><code class="language-bash">  User.java
package com.itheima.mybatis.pojo<span class="token punctuation">;</span>
<span class="token function">import</span> java.util.Date<span class="token punctuation">;</span>
public class User <span class="token punctuation">{</span>
    private Integer <span class="token function">id</span><span class="token punctuation">;</span>

    private String username<span class="token punctuation">;</span>

    private Date birthday<span class="token punctuation">;</span>

    private String sex<span class="token punctuation">;</span>

    private String address<span class="token punctuation">;</span>

    public Integer getId<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">id</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public void setId<span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.id <span class="token operator">=</span> <span class="token function">id</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public String getUsername<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> username<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public void setUsername<span class="token punctuation">(</span>String username<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.username <span class="token operator">=</span> username <span class="token operator">==</span> null ? null <span class="token keyword">:</span> username.trim<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public Date getBirthday<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> birthday<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public void setBirthday<span class="token punctuation">(</span>Date birthday<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.birthday <span class="token operator">=</span> birthday<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public String getSex<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> sex<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public void setSex<span class="token punctuation">(</span>String sex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.sex <span class="token operator">=</span> sex <span class="token operator">==</span> null ? null <span class="token keyword">:</span> sex.trim<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public String getAddress<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> address<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    public void setAddress<span class="token punctuation">(</span>String address<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.address <span class="token operator">=</span> address <span class="token operator">==</span> null ? null <span class="token keyword">:</span> address.trim<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="mapper"><a href="#mapper" class="headerlink" title="mapper"></a>mapper</h3><pre class=" language-bash"><code class="language-bash"> UserMapper.java
 package com.itheima.mybatis.mapper<span class="token punctuation">;</span>

<span class="token function">import</span> com.itheima.mybatis.pojo.User<span class="token punctuation">;</span>
<span class="token function">import</span> com.itheima.mybatis.pojo.UserExample<span class="token punctuation">;</span>
<span class="token function">import</span> java.util.List<span class="token punctuation">;</span>
<span class="token function">import</span> org.apache.ibatis.annotations.Param<span class="token punctuation">;</span>

public interface UserMapper <span class="token punctuation">{</span>
    int countByExample<span class="token punctuation">(</span>UserExample example<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int deleteByExample<span class="token punctuation">(</span>UserExample example<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int deleteByPrimaryKey<span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int insert<span class="token punctuation">(</span>User record<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int insertSelective<span class="token punctuation">(</span>User record<span class="token punctuation">)</span><span class="token punctuation">;</span>

    List<span class="token operator">&lt;</span>User<span class="token operator">></span> selectByExample<span class="token punctuation">(</span>UserExample example<span class="token punctuation">)</span><span class="token punctuation">;</span>

    User selectByPrimaryKey<span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int updateByExampleSelective<span class="token punctuation">(</span>@Param<span class="token punctuation">(</span><span class="token string">"record"</span><span class="token punctuation">)</span> User record, @Param<span class="token punctuation">(</span><span class="token string">"example"</span><span class="token punctuation">)</span> UserExample example<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int updateByExample<span class="token punctuation">(</span>@Param<span class="token punctuation">(</span><span class="token string">"record"</span><span class="token punctuation">)</span> User record, @Param<span class="token punctuation">(</span><span class="token string">"example"</span><span class="token punctuation">)</span> UserExample example<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int updateByPrimaryKeySelective<span class="token punctuation">(</span>User record<span class="token punctuation">)</span><span class="token punctuation">;</span>

    int updateByPrimaryKey<span class="token punctuation">(</span>User record<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">  mapper.xml
  <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span> ?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE mapper PUBLIC <span class="token string">"-//mybatis.org//DTD Mapper 3.0//EN"</span> <span class="token string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span> <span class="token operator">></span>
<span class="token operator">&lt;</span>mapper namespace<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.mapper.UserMapper"</span> <span class="token operator">></span>
  <span class="token operator">&lt;</span>resultMap id<span class="token operator">=</span><span class="token string">"BaseResultMap"</span> type<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>id column<span class="token operator">=</span><span class="token string">"id"</span> property<span class="token operator">=</span><span class="token string">"id"</span> jdbcType<span class="token operator">=</span><span class="token string">"INTEGER"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>result column<span class="token operator">=</span><span class="token string">"username"</span> property<span class="token operator">=</span><span class="token string">"username"</span> jdbcType<span class="token operator">=</span><span class="token string">"VARCHAR"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>result column<span class="token operator">=</span><span class="token string">"birthday"</span> property<span class="token operator">=</span><span class="token string">"birthday"</span> jdbcType<span class="token operator">=</span><span class="token string">"DATE"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>result column<span class="token operator">=</span><span class="token string">"sex"</span> property<span class="token operator">=</span><span class="token string">"sex"</span> jdbcType<span class="token operator">=</span><span class="token string">"CHAR"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>result column<span class="token operator">=</span><span class="token string">"address"</span> property<span class="token operator">=</span><span class="token string">"address"</span> jdbcType<span class="token operator">=</span><span class="token string">"VARCHAR"</span> /<span class="token operator">></span>
  <span class="token operator">&lt;</span>/resultMap<span class="token operator">></span>
  <span class="token operator">&lt;</span>sql id<span class="token operator">=</span><span class="token string">"Example_Where_Clause"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>where <span class="token operator">></span>
      <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"oredCriteria"</span> item<span class="token operator">=</span><span class="token string">"criteria"</span> separator<span class="token operator">=</span><span class="token string">"or"</span> <span class="token operator">></span>
        <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"criteria.valid"</span> <span class="token operator">></span>
          <span class="token operator">&lt;</span>trim prefix<span class="token operator">=</span><span class="token string">"("</span> suffix<span class="token operator">=</span><span class="token string">")"</span> prefixOverrides<span class="token operator">=</span><span class="token string">"and"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"criteria.criteria"</span> item<span class="token operator">=</span><span class="token string">"criterion"</span> <span class="token operator">></span>
              <span class="token operator">&lt;</span>choose <span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.noValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.singleValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span> <span class="token comment" spellcheck="true">#{criterion.value}</span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.betweenValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span> <span class="token comment" spellcheck="true">#{criterion.value} and #{criterion.secondValue}</span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.listValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span>
                  <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"criterion.value"</span> item<span class="token operator">=</span><span class="token string">"listItem"</span> open<span class="token operator">=</span><span class="token string">"("</span> close<span class="token operator">=</span><span class="token string">")"</span> separator<span class="token operator">=</span><span class="token string">","</span> <span class="token operator">></span>
                    <span class="token comment" spellcheck="true">#{listItem}</span>
                  <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
              <span class="token operator">&lt;</span>/choose<span class="token operator">></span>
            <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
          <span class="token operator">&lt;</span>/trim<span class="token operator">></span>
        <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
    <span class="token operator">&lt;</span>/where<span class="token operator">></span>
  <span class="token operator">&lt;</span>/sql<span class="token operator">></span>
  <span class="token operator">&lt;</span>sql id<span class="token operator">=</span><span class="token string">"Update_By_Example_Where_Clause"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>where <span class="token operator">></span>
      <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"example.oredCriteria"</span> item<span class="token operator">=</span><span class="token string">"criteria"</span> separator<span class="token operator">=</span><span class="token string">"or"</span> <span class="token operator">></span>
        <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"criteria.valid"</span> <span class="token operator">></span>
          <span class="token operator">&lt;</span>trim prefix<span class="token operator">=</span><span class="token string">"("</span> suffix<span class="token operator">=</span><span class="token string">")"</span> prefixOverrides<span class="token operator">=</span><span class="token string">"and"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"criteria.criteria"</span> item<span class="token operator">=</span><span class="token string">"criterion"</span> <span class="token operator">></span>
              <span class="token operator">&lt;</span>choose <span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.noValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.singleValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span> <span class="token comment" spellcheck="true">#{criterion.value}</span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.betweenValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span> <span class="token comment" spellcheck="true">#{criterion.value} and #{criterion.secondValue}</span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
                <span class="token operator">&lt;</span>when test<span class="token operator">=</span><span class="token string">"criterion.listValue"</span> <span class="token operator">></span>
                  and <span class="token variable">${criterion.condition}</span>
                  <span class="token operator">&lt;</span>foreach collection<span class="token operator">=</span><span class="token string">"criterion.value"</span> item<span class="token operator">=</span><span class="token string">"listItem"</span> open<span class="token operator">=</span><span class="token string">"("</span> close<span class="token operator">=</span><span class="token string">")"</span> separator<span class="token operator">=</span><span class="token string">","</span> <span class="token operator">></span>
                    <span class="token comment" spellcheck="true">#{listItem}</span>
                  <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
                <span class="token operator">&lt;</span>/when<span class="token operator">></span>
              <span class="token operator">&lt;</span>/choose<span class="token operator">></span>
            <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
          <span class="token operator">&lt;</span>/trim<span class="token operator">></span>
        <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>/foreach<span class="token operator">></span>
    <span class="token operator">&lt;</span>/where<span class="token operator">></span>
  <span class="token operator">&lt;</span>/sql<span class="token operator">></span>
  <span class="token operator">&lt;</span>sql id<span class="token operator">=</span><span class="token string">"Base_Column_List"</span> <span class="token operator">></span>
    id, username, birthday, sex, address
  <span class="token operator">&lt;</span>/sql<span class="token operator">></span>
  <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"selectByExample"</span> resultMap<span class="token operator">=</span><span class="token string">"BaseResultMap"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.UserExample"</span> <span class="token operator">></span>
    <span class="token keyword">select</span>
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"distinct"</span> <span class="token operator">></span>
      distinct
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
    <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Base_Column_List"</span> /<span class="token operator">></span>
    from user
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"_parameter != null"</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Example_Where_Clause"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"orderByClause != null"</span> <span class="token operator">></span>
      order by <span class="token variable">${orderByClause}</span>
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
  <span class="token operator">&lt;</span>/select<span class="token operator">></span>
  <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"selectByPrimaryKey"</span> resultMap<span class="token operator">=</span><span class="token string">"BaseResultMap"</span> parameterType<span class="token operator">=</span><span class="token string">"java.lang.Integer"</span> <span class="token operator">></span>
    <span class="token keyword">select</span> 
    <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Base_Column_List"</span> /<span class="token operator">></span>
    from user
    where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{id,jdbcType=INTEGER}</span>
  <span class="token operator">&lt;</span>/select<span class="token operator">></span>
  <span class="token operator">&lt;</span>delete id<span class="token operator">=</span><span class="token string">"deleteByPrimaryKey"</span> parameterType<span class="token operator">=</span><span class="token string">"java.lang.Integer"</span> <span class="token operator">></span>
    delete from user
    where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{id,jdbcType=INTEGER}</span>
  <span class="token operator">&lt;</span>/delete<span class="token operator">></span>
  <span class="token operator">&lt;</span>delete id<span class="token operator">=</span><span class="token string">"deleteByExample"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.UserExample"</span> <span class="token operator">></span>
    delete from user
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"_parameter != null"</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Example_Where_Clause"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
  <span class="token operator">&lt;</span>/delete<span class="token operator">></span>
  <span class="token operator">&lt;</span>insert id<span class="token operator">=</span><span class="token string">"insert"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span> <span class="token operator">></span>
    insert into user <span class="token punctuation">(</span>id, username, birthday, 
      sex, address<span class="token punctuation">)</span>
    values <span class="token punctuation">(</span><span class="token comment" spellcheck="true">#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, </span>
      <span class="token comment" spellcheck="true">#{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR})</span>
  <span class="token operator">&lt;</span>/insert<span class="token operator">></span>
  <span class="token operator">&lt;</span>insert id<span class="token operator">=</span><span class="token string">"insertSelective"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span> <span class="token operator">></span>
    insert into user
    <span class="token operator">&lt;</span>trim prefix<span class="token operator">=</span><span class="token string">"("</span> suffix<span class="token operator">=</span><span class="token string">")"</span> suffixOverrides<span class="token operator">=</span><span class="token string">","</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"id != null"</span> <span class="token operator">></span>
        id,
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"username != null"</span> <span class="token operator">></span>
        username,
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"birthday != null"</span> <span class="token operator">></span>
        birthday,
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"sex != null"</span> <span class="token operator">></span>
        sex,
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"address != null"</span> <span class="token operator">></span>
        address,
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
    <span class="token operator">&lt;</span>/trim<span class="token operator">></span>
    <span class="token operator">&lt;</span>trim prefix<span class="token operator">=</span><span class="token string">"values ("</span> suffix<span class="token operator">=</span><span class="token string">")"</span> suffixOverrides<span class="token operator">=</span><span class="token string">","</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"id != null"</span> <span class="token operator">></span>
        <span class="token comment" spellcheck="true">#{id,jdbcType=INTEGER},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"username != null"</span> <span class="token operator">></span>
        <span class="token comment" spellcheck="true">#{username,jdbcType=VARCHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"birthday != null"</span> <span class="token operator">></span>
        <span class="token comment" spellcheck="true">#{birthday,jdbcType=DATE},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"sex != null"</span> <span class="token operator">></span>
        <span class="token comment" spellcheck="true">#{sex,jdbcType=CHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"address != null"</span> <span class="token operator">></span>
        <span class="token comment" spellcheck="true">#{address,jdbcType=VARCHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
    <span class="token operator">&lt;</span>/trim<span class="token operator">></span>
  <span class="token operator">&lt;</span>/insert<span class="token operator">></span>
  <span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">"countByExample"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.UserExample"</span> resultType<span class="token operator">=</span><span class="token string">"java.lang.Integer"</span> <span class="token operator">></span>
    <span class="token keyword">select</span> count<span class="token punctuation">(</span>*<span class="token punctuation">)</span> from user
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"_parameter != null"</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Example_Where_Clause"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
  <span class="token operator">&lt;</span>/select<span class="token operator">></span>
  <span class="token operator">&lt;</span>update id<span class="token operator">=</span><span class="token string">"updateByExampleSelective"</span> parameterType<span class="token operator">=</span><span class="token string">"map"</span> <span class="token operator">></span>
    update user
    <span class="token operator">&lt;</span>set <span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"record.id != null"</span> <span class="token operator">></span>
        <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.id,jdbcType=INTEGER},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"record.username != null"</span> <span class="token operator">></span>
        username <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.username,jdbcType=VARCHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"record.birthday != null"</span> <span class="token operator">></span>
        birthday <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.birthday,jdbcType=DATE},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"record.sex != null"</span> <span class="token operator">></span>
        sex <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.sex,jdbcType=CHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"record.address != null"</span> <span class="token operator">></span>
        address <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.address,jdbcType=VARCHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
    <span class="token operator">&lt;</span>/set<span class="token operator">></span>
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"_parameter != null"</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Update_By_Example_Where_Clause"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
  <span class="token operator">&lt;</span>/update<span class="token operator">></span>
  <span class="token operator">&lt;</span>update id<span class="token operator">=</span><span class="token string">"updateByExample"</span> parameterType<span class="token operator">=</span><span class="token string">"map"</span> <span class="token operator">></span>
    update user
    <span class="token keyword">set</span> <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.id,jdbcType=INTEGER},</span>
      username <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.username,jdbcType=VARCHAR},</span>
      birthday <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.birthday,jdbcType=DATE},</span>
      sex <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.sex,jdbcType=CHAR},</span>
      address <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{record.address,jdbcType=VARCHAR}</span>
    <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"_parameter != null"</span> <span class="token operator">></span>
      <span class="token operator">&lt;</span>include refid<span class="token operator">=</span><span class="token string">"Update_By_Example_Where_Clause"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/if<span class="token operator">></span>
  <span class="token operator">&lt;</span>/update<span class="token operator">></span>
  <span class="token operator">&lt;</span>update id<span class="token operator">=</span><span class="token string">"updateByPrimaryKeySelective"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span> <span class="token operator">></span>
    update user
    <span class="token operator">&lt;</span>set <span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"username != null"</span> <span class="token operator">></span>
        username <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{username,jdbcType=VARCHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"birthday != null"</span> <span class="token operator">></span>
        birthday <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{birthday,jdbcType=DATE},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"sex != null"</span> <span class="token operator">></span>
        sex <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{sex,jdbcType=CHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
      <span class="token operator">&lt;</span>if test<span class="token operator">=</span><span class="token string">"address != null"</span> <span class="token operator">></span>
        address <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{address,jdbcType=VARCHAR},</span>
      <span class="token operator">&lt;</span>/if<span class="token operator">></span>
    <span class="token operator">&lt;</span>/set<span class="token operator">></span>
    where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{id,jdbcType=INTEGER}</span>
  <span class="token operator">&lt;</span>/update<span class="token operator">></span>
  <span class="token operator">&lt;</span>update id<span class="token operator">=</span><span class="token string">"updateByPrimaryKey"</span> parameterType<span class="token operator">=</span><span class="token string">"com.itheima.mybatis.pojo.User"</span> <span class="token operator">></span>
    update user
    <span class="token keyword">set</span> username <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{username,jdbcType=VARCHAR},</span>
      birthday <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{birthday,jdbcType=DATE},</span>
      sex <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{sex,jdbcType=CHAR},</span>
      address <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{address,jdbcType=VARCHAR}</span>
    where <span class="token function">id</span> <span class="token operator">=</span> <span class="token comment" spellcheck="true">#{id,jdbcType=INTEGER}</span>
  <span class="token operator">&lt;</span>/update<span class="token operator">></span>
<span class="token operator">&lt;</span>/mapper<span class="token operator">></span>
</code></pre>
<h3 id="Test"><a href="#Test" class="headerlink" title="Test"></a>Test</h3><pre class=" language-bash"><code class="language-bash">  package com.itheima.mybatis.junit<span class="token punctuation">;</span>

<span class="token function">import</span> java.util.List<span class="token punctuation">;</span>

<span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.ApplicationContext<span class="token punctuation">;</span>
<span class="token function">import</span> org.springframework.context.support.ClassPathXmlApplicationContext<span class="token punctuation">;</span>

<span class="token function">import</span> com.itheima.mybatis.mapper.UserMapper<span class="token punctuation">;</span>
<span class="token function">import</span> com.itheima.mybatis.pojo.User<span class="token punctuation">;</span>
<span class="token function">import</span> com.itheima.mybatis.pojo.UserExample<span class="token punctuation">;</span>

public class JunitTest <span class="token punctuation">{</span>
    @Test
    public void testMapper<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span>
        ApplicationContext ac <span class="token operator">=</span> new ClassPathXmlApplicationContext<span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        UserMapper userMapper <span class="token operator">=</span> ac.getBean<span class="token punctuation">(</span>UserMapper.class<span class="token punctuation">)</span><span class="token punctuation">;</span>
        UserExample example <span class="token operator">=</span> new UserExample<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        String username <span class="token operator">=</span> <span class="token string">"明"</span><span class="token punctuation">;</span>
        example.createCriteria<span class="token punctuation">(</span><span class="token punctuation">)</span>.andSexEqualTo<span class="token punctuation">(</span><span class="token string">"1"</span><span class="token punctuation">)</span>.andUsernameLike<span class="token punctuation">(</span><span class="token string">"%"</span> + username + <span class="token string">"%"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        example.setOrderByClause<span class="token punctuation">(</span><span class="token string">"id desc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        int countByExample <span class="token operator">=</span> userMapper.countByExample<span class="token punctuation">(</span>example<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>countByExample<span class="token punctuation">)</span><span class="token punctuation">;</span>

        User user <span class="token operator">=</span> userMapper.selectByPrimaryKey<span class="token punctuation">(</span>10<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>


        List<span class="token operator">&lt;</span>User<span class="token operator">></span> <span class="token function">users</span> <span class="token operator">=</span> userMapper.selectByExample<span class="token punctuation">(</span>example<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span>User user2 <span class="token keyword">:</span> users<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            System.out.println<span class="token punctuation">(</span>user2.getId<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="springmvc-SSM整合Demo"><a href="#springmvc-SSM整合Demo" class="headerlink" title="springmvc/SSM整合Demo"></a>springmvc/SSM整合Demo</h2><h3 id="springmvc-xml"><a href="#springmvc-xml" class="headerlink" title="springmvc.xml"></a>springmvc.xml</h3><pre class=" language-bash"><code class="language-bash">  <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span>
    xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns:p<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/p"</span>
    xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span>
    xmlns:mvc<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/mvc"</span>
    xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"</span><span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 扫描@Controler  @Service   --<span class="token operator">></span>
        <span class="token operator">&lt;</span>context:component-scan base-package<span class="token operator">=</span><span class="token string">"com.itheima"</span>/<span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 处理器映射器 --<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>--         <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"</span>/<span class="token operator">></span> --<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 处理器适配器 --<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>--         <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"</span>/<span class="token operator">></span> --<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 注解驱动 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>mvc:annotation-driven conversion-service<span class="token operator">=</span><span class="token string">"conversionServiceFactoryBean"</span>/<span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置Conveter转换器  转换工厂 （日期、去掉前后空格）。。 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"conversionServiceFactoryBean"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.format.support.FormattingConversionServiceFactoryBean"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置 多个转换器--<span class="token operator">></span>
            <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"converters"</span><span class="token operator">></span>
                <span class="token operator">&lt;</span>list<span class="token operator">></span>
                    <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"com.itheima.springmvc.conversion.DateConveter"</span>/<span class="token operator">></span>
                <span class="token operator">&lt;</span>/list<span class="token operator">></span>
            <span class="token operator">&lt;</span>/property<span class="token operator">></span>
        <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 上传图片配置实现类 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"multipartResolver"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.web.multipart.commons.CommonsMultipartResolver"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 上传图片的大小   B   5M  1*1024*1024*5--<span class="token operator">></span>
            <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxUploadSize"</span> value<span class="token operator">=</span><span class="token string">"5000000"</span>/<span class="token operator">></span>
        <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- Springmvc的异常处理器 --<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>--         <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"com.itheima.springmvc.exception.CustomExceptionResolver"</span>/<span class="token operator">></span> --<span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- SPringmvc的拦截器 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>mvc:interceptors<span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- 多个拦截器 --<span class="token operator">></span>
            <span class="token operator">&lt;</span>mvc:interceptor<span class="token operator">></span>
                <span class="token operator">&lt;</span>mvc:mapping path<span class="token operator">=</span><span class="token string">"/**"</span>/<span class="token operator">></span>
                <span class="token operator">&lt;</span><span class="token operator">!</span>-- 自定义的拦截器类 --<span class="token operator">></span>
                <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"com.itheima.springmvc.interceptor.Interceptor1"</span>/<span class="token operator">></span>
            <span class="token operator">&lt;</span>/mvc:interceptor<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>--         <span class="token operator">&lt;</span>mvc:interceptor<span class="token operator">></span>
                <span class="token operator">&lt;</span>mvc:mapping path<span class="token operator">=</span><span class="token string">"/**"</span>/<span class="token operator">></span>
                自定义的拦截器类
                <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"com.itheima.springmvc.interceptor.Interceptor2"</span>/<span class="token operator">></span>
            <span class="token operator">&lt;</span>/mvc:interceptor<span class="token operator">></span> --<span class="token operator">></span>
        <span class="token operator">&lt;</span>/mvc:interceptors<span class="token operator">></span>

        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 视图解释器 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"prefix"</span> value<span class="token operator">=</span><span class="token string">"/WEB-INF/jsp/"</span>/<span class="token operator">></span>
            <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"suffix"</span> value<span class="token operator">=</span><span class="token string">".jsp"</span>/<span class="token operator">></span>
        <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

   <span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<h3 id="applicationContext-xml-1"><a href="#applicationContext-xml-1" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h3><pre class=" language-bash"><code class="language-bash">  <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>beans xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span>
    xmlns:context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xmlns:p<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/p"</span>
    xmlns:aop<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/aop"</span> xmlns:tx<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/tx"</span>
    xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span>
    xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"</span><span class="token operator">></span>


    <span class="token operator">&lt;</span>context:property-placeholder location<span class="token operator">=</span><span class="token string">"classpath:db.properties"</span>/<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 数据库连接池 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"dataSource"</span> class<span class="token operator">=</span><span class="token string">"org.apache.commons.dbcp.BasicDataSource"</span>
        destroy-method<span class="token operator">=</span><span class="token string">"close"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"driverClassName"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.driver}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"url"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.url}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"username"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.username}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"password"</span> value<span class="token operator">=</span><span class="token string">"<span class="token variable">${jdbc.password}</span>"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxActive"</span> value<span class="token operator">=</span><span class="token string">"10"</span> /<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxIdle"</span> value<span class="token operator">=</span><span class="token string">"5"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- Mybatis的工厂 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"sqlSessionFactoryBean"</span> class<span class="token operator">=</span><span class="token string">"org.mybatis.spring.SqlSessionFactoryBean"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span>/<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 核心配置文件的位置 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"configLocation"</span> value<span class="token operator">=</span><span class="token string">"classpath:sqlMapConfig.xml"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- Mapper动态代理开发   扫描 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean class<span class="token operator">=</span><span class="token string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 基本包 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"basePackage"</span> value<span class="token operator">=</span><span class="token string">"com.itheima.springmvc.dao"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 注解事务 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"transactionManager"</span> class<span class="token operator">=</span><span class="token string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"dataSource"</span> ref<span class="token operator">=</span><span class="token string">"dataSource"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>/bean<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 开启注解 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>tx:annotation-driven transaction-manager<span class="token operator">=</span><span class="token string">"transactionManager"</span>/<span class="token operator">></span>
<span class="token operator">&lt;</span>/beans<span class="token operator">></span>
</code></pre>
<h3 id="sqlMapConfig-xml-1"><a href="#sqlMapConfig-xml-1" class="headerlink" title="sqlMapConfig.xml"></a>sqlMapConfig.xml</h3><pre class=" language-bash"><code class="language-bash">    <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span> ?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE configuration
PUBLIC <span class="token string">"-//mybatis.org//DTD Config 3.0//EN"</span>
<span class="token string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>configuration<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 设置别名 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>typeAliases<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 2. 指定扫描包，会把包内所有的类都设置别名，别名的名称就是类名，大小写不敏感 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"com.itheima.springmvc.pojo"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/typeAliases<span class="token operator">></span>
<span class="token operator">&lt;</span>/configuration<span class="token operator">></span>
</code></pre>
<h3 id="SSM-整合代码下载"><a href="#SSM-整合代码下载" class="headerlink" title="SSM 整合代码下载"></a>SSM 整合代码下载</h3><p> 一个小demo 对SSM整合</p>
<p>   <img src="/2019/01/24/ssm-fu-xi-zong-jie/14.jpg" alt="图14"></p>
<p>  github代码下载：   <a href="https://github.com/zy20081/JAVA/tree/master/springmvc-mybatis02" target="_blank" rel="noopener">https://github.com/zy20081/JAVA/tree/master/springmvc-mybatis02</a></p>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff;
        background-color: #22AB38;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff;
        background-color: #019FE8;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a class="reward-link btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs">
                        <li class="tab wechat-tab waves-effect waves-light"><a class="active" href="#wechat">微信</a></li>
                        <li class="tab alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                    </ul>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('#reward .reward-link').on('click', function () {
            $('#rewardModal').openModal();
        });

        $('#rewardModal .close').on('click', function () {
            $('#rewardModal').closeModal();
        });
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://zy20081.github.io" class="b-link-green">Zhou•Yong</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/2019/01/24/ssm-fu-xi-zong-jie/" class="b-link-green">SSM 复习总结</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">上一篇</div>
            <div class="card">
                <a href="/2019/01/28/git-xia-zai-mou-ge-wen-jian-jia/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/4.jpg" class="responsive-img" alt="Git下载某个文件夹">
                        
                        <span class="card-title">Git下载某个文件夹</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">方法一需要工具：TortoiseSVN 
操作举例：

例如，你只需要“NLPIR SDK”中“classify”文件夹下的代码，先打开 classify,其 URL 为: “https://github.com/NLPIR-team/NL</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2019-01-28
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/工具/" class="post-category" target="_blank">
                                    工具
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/工具/" target="_blank">
                        <span class="chip bg-color">工具</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/2019/01/17/shang-chuan-yu-xia-zai/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="上传与下载">
                        
                        <span class="card-title">上传与下载</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">上传与下载听说好多面试的时候喜欢出上传与下载的题目，这里我就写winform版本和.net版本都写一个，写核心代码，细节就不写了，仅供参考
Form
 //上传
        private void btnUpload_Click(ob</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-01-17
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/ASP-NET/" class="post-category" target="_blank">
                                    ASP.NET
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/ASP-NET/" target="_blank">
                        <span class="chip bg-color">ASP.NET</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title">目录</div>
            <div id="toc-content">

            </div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由ZhouYong 技术分享，心得分享.
        </div>
        <div class="col s12 m4 l4 social-link"><a href="#!" class="tooltipped" data-tooltip="邮件联系我: zy20081@126.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 314153271" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input" autofocus>
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>

<script src="/libs/others/clicklove.js"></script>

<script src="/js/matery.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->


</body>
</html>